/*
 * Interface gráfica de consulta de faltas por um aluno.
 */
package amanda_gabriel_isabella_lucivania_monica.View;

import amanda_gabriel_isabella_lucivania_monica.Model.Dao.DaoBaseImpl;
import amanda_gabriel_isabella_lucivania_monica.Model.Pojo.Aluno;
import amanda_gabriel_isabella_lucivania_monica.Model.Pojo.Faltas;
import amanda_gabriel_isabella_lucivania_monica.Model.Pojo.Turma;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

/**
 * Interface gráfica de consultar faltas
 *
 * @author lucy
 */
public class ConsultarFaltasIG extends javax.swing.JFrame {

    private DaoBaseImpl daoBaseImpl;

    /**
     * Cria um novo form ConsultarFaltasIG
     */
    public ConsultarFaltasIG(DaoBaseImpl daoBaseImpl) {
        initComponents();
        jTResultadoFaltas.setEditable(false);
        this.daoBaseImpl = daoBaseImpl;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jDCamposVazios = new javax.swing.JDialog();
        jLabel4 = new javax.swing.JLabel();
        jBOK = new javax.swing.JButton();
        amanda_gabriel_isabella_lucivania_monicaPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("amanda_gabriel_isabella_lucivania_monicaPU").createEntityManager();
        alunoQuery = java.beans.Beans.isDesignTime() ? null : amanda_gabriel_isabella_lucivania_monicaPUEntityManager.createQuery("SELECT a FROM Aluno a");
        alunoList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : alunoQuery.getResultList();
        turmaQuery = java.beans.Beans.isDesignTime() ? null : amanda_gabriel_isabella_lucivania_monicaPUEntityManager.createQuery("SELECT t FROM Turma t");
        turmaList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : turmaQuery.getResultList();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jCTurma = new javax.swing.JComboBox();
        jCNome = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jTResultadoFaltas = new javax.swing.JTextField();
        jBConsultar = new javax.swing.JButton();
        jBCancelar = new javax.swing.JButton();

        jDCamposVazios.setTitle("ATENÇÃO");
        jDCamposVazios.setMinimumSize(new java.awt.Dimension(400, 175));

        jLabel4.setText("Todos os campos devem ser preenchidos!");

        jBOK.setText("OK");
        jBOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBOKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jDCamposVaziosLayout = new javax.swing.GroupLayout(jDCamposVazios.getContentPane());
        jDCamposVazios.getContentPane().setLayout(jDCamposVaziosLayout);
        jDCamposVaziosLayout.setHorizontalGroup(
            jDCamposVaziosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDCamposVaziosLayout.createSequentialGroup()
                .addGroup(jDCamposVaziosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jDCamposVaziosLayout.createSequentialGroup()
                        .addGap(75, 75, 75)
                        .addComponent(jLabel4))
                    .addGroup(jDCamposVaziosLayout.createSequentialGroup()
                        .addGap(151, 151, 151)
                        .addComponent(jBOK, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        jDCamposVaziosLayout.setVerticalGroup(
            jDCamposVaziosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDCamposVaziosLayout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(jLabel4)
                .addGap(31, 31, 31)
                .addComponent(jBOK)
                .addContainerGap(33, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Consultar faltas");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Informe os dados abaixo:"));

        jLabel1.setText("Nome:");

        jLabel2.setText("Turma:");

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, turmaList, jCTurma);
        bindingGroup.addBinding(jComboBoxBinding);

        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, alunoList, jCNome);
        bindingGroup.addBinding(jComboBoxBinding);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jCNome, javax.swing.GroupLayout.PREFERRED_SIZE, 253, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(jCTurma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jCNome, jCTurma});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jCNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jCTurma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Resultado:"));

        jLabel3.setText("Quantidade de faltas nesta turma:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jTResultadoFaltas, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(39, 39, 39))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTResultadoFaltas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(55, Short.MAX_VALUE))
        );

        jBConsultar.setText("Consultar");
        jBConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBConsultarActionPerformed(evt);
            }
        });

        jBCancelar.setText("Cancelar");
        jBCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jBCancelar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jBConsultar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jBCancelar, jBConsultar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(34, 34, 34)
                .addComponent(jBConsultar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31)
                .addComponent(jBCancelar)
                .addGap(25, 25, 25))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBCancelarActionPerformed
        setVisible(false);
    }//GEN-LAST:event_jBCancelarActionPerformed

    private void jBConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBConsultarActionPerformed
        Aluno aluno = (Aluno) jCNome.getSelectedItem();
        Turma turma = (Turma) jCTurma.getSelectedItem();

        if ((jCNome.getSelectedIndex() == -1) || (jCTurma.getSelectedIndex() == -1)) {
            jDCamposVazios.setVisible(true);
            setVisible(false);
        } else {
            //String query = "select f from Aluno a join a.faltas f join f.turma t where t.id = :idTurma and a.idPessoa = :idAluno";
             String query = "select f from Aluno a join a.faltas f join f.turma t where t.id = :idTurma and a.idPessoa = :idAluno";
            List<Integer> dados = new ArrayList<Integer>();
            List<String> parametros = new ArrayList<String>();

            dados.add(turma.getId());
            parametros.add("idTurma");

            dados.add(aluno.getIdPessoa());
            parametros.add("idAluno");
            List<Objects> listaResultados = (List<Objects>) daoBaseImpl.executarQuery(query, dados,parametros);

            if (listaResultados.size() == 1) {

                for (Iterator<Objects> it = listaResultados.iterator(); it.hasNext();) {
                    Object objects = it.next();
                    Faltas faltas = (Faltas) objects;
                    jTResultadoFaltas.setText("" + faltas.getNumeroFaltas());
                }

            }
            //TODO fazer teste se retorna mais de um registro
        }

    }//GEN-LAST:event_jBConsultarActionPerformed

    private void jBOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBOKActionPerformed
        jDCamposVazios.dispose();
        setVisible(true);
    }//GEN-LAST:event_jBOKActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.util.List<amanda_gabriel_isabella_lucivania_monica.Model.Pojo.Aluno> alunoList;
    private javax.persistence.Query alunoQuery;
    private javax.persistence.EntityManager amanda_gabriel_isabella_lucivania_monicaPUEntityManager;
    private javax.swing.JButton jBCancelar;
    private javax.swing.JButton jBConsultar;
    private javax.swing.JButton jBOK;
    private javax.swing.JComboBox jCNome;
    private javax.swing.JComboBox jCTurma;
    private javax.swing.JDialog jDCamposVazios;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTResultadoFaltas;
    private java.util.List<amanda_gabriel_isabella_lucivania_monica.Model.Pojo.Turma> turmaList;
    private javax.persistence.Query turmaQuery;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
